<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.4"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Flow-IPC: ipc::util::Default_init_allocator&lt; T, Allocator &gt; Class Template Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">Flow-IPC<span id="projectnumber">&#160;1.0.2</span>
   </div>
   <div id="projectbrief">Flow-IPC project: Full implementation reference.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="namespaceipc.html">ipc</a></li><li class="navelem"><a class="el" href="namespaceipc_1_1util.html">util</a></li><li class="navelem"><a class="el" href="classipc_1_1util_1_1Default__init__allocator.html">Default_init_allocator</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="classipc_1_1util_1_1Default__init__allocator-members.html">List of all members</a>  </div>
  <div class="headertitle"><div class="title">ipc::util::Default_init_allocator&lt; T, Allocator &gt; Class Template Reference</div></div>
</div><!--header-->
<div class="contents">

<p><a class="el" href="classAllocator.html">Allocator</a> adaptor (useful for, e.g., <code>vector</code> that skips zero-filling) that turns a value-initialization <code>T()</code> into a default-initialization for those types, namely PoDs, for which default-initialization is a no-op.  
 <a href="classipc_1_1util_1_1Default__init__allocator.html#details">More...</a></p>

<p><code>#include &lt;<a class="el" href="default__init__allocator_8hpp_source.html">default_init_allocator.hpp</a>&gt;</code></p>
<div class="dynheader">
Inheritance diagram for ipc::util::Default_init_allocator&lt; T, Allocator &gt;:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="classipc_1_1util_1_1Default__init__allocator__inherit__graph.svg" width="130" height="131"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<div class="dynheader">
Collaboration diagram for ipc::util::Default_init_allocator&lt; T, Allocator &gt;:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="classipc_1_1util_1_1Default__init__allocator__coll__graph.svg" width="130" height="131"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pub-methods" name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:a56c4ab4a0b12a26f177c35d50b96f27f"><td class="memTemplParams" colspan="2">template&lt;typename U &gt; </td></tr>
<tr class="memitem:a56c4ab4a0b12a26f177c35d50b96f27f"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="classipc_1_1util_1_1Default__init__allocator.html#a56c4ab4a0b12a26f177c35d50b96f27f">construct</a> (U *ptr) noexcept(std::is_nothrow_default_constructible_v&lt; U &gt;)</td></tr>
<tr class="memdesc:a56c4ab4a0b12a26f177c35d50b96f27f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Satisfies <code><a class="el" href="classAllocator.html">Allocator</a></code> concept optional requirement for in-place construction: specialized for 0-args, i.e., value-initialization; replaces value-initialization with default-initialization.  <a href="classipc_1_1util_1_1Default__init__allocator.html#a56c4ab4a0b12a26f177c35d50b96f27f">More...</a><br /></td></tr>
<tr class="separator:a56c4ab4a0b12a26f177c35d50b96f27f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac030ddf85696b12dc0429b25bf5c6100"><td class="memTemplParams" colspan="2">template&lt;typename U , typename... Args&gt; </td></tr>
<tr class="memitem:ac030ddf85696b12dc0429b25bf5c6100"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="classipc_1_1util_1_1Default__init__allocator.html#ac030ddf85696b12dc0429b25bf5c6100">construct</a> (U *ptr, Args &amp;&amp;... args)</td></tr>
<tr class="memdesc:ac030ddf85696b12dc0429b25bf5c6100"><td class="mdescLeft">&#160;</td><td class="mdescRight">Satisfies <code><a class="el" href="classAllocator.html">Allocator</a></code> concept optional requirement for in-place construction: non-specialized version invoked for 1+ args.  <a href="classipc_1_1util_1_1Default__init__allocator.html#ac030ddf85696b12dc0429b25bf5c6100">More...</a><br /></td></tr>
<tr class="separator:ac030ddf85696b12dc0429b25bf5c6100"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><div class="compoundTemplParams">template&lt;typename T, typename <a class="el" href="classAllocator.html">Allocator</a>&gt;<br />
class ipc::util::Default_init_allocator&lt; T, Allocator &gt;</div><p ><a class="el" href="classAllocator.html">Allocator</a> adaptor (useful for, e.g., <code>vector</code> that skips zero-filling) that turns a value-initialization <code>T()</code> into a default-initialization for those types, namely PoDs, for which default-initialization is a no-op. </p>
<h3>Rationale (why)</h3>
<p >Suppose you have <code>vector&lt;uint8_t&gt; v(10);</code>. This will create a buffer and fill with at least 10 zeroes. What if you want it <em>not</em> do this? Maybe you don't want it for performane. Actually, consider an even sneakier situation that involves correctness, not just perf. Suppose you have <code>vector&lt;uint8_t&gt; v</code> with <code>.capacity() == 10</code> and <code>.size() == 0</code>, but you're letting some 3rd-party code operate in range <code>[v.begin(), v.begin() + v.capacity())</code>, and you're using <code>.size()</code> as a store of the result the 3rd-party operation. Say the 3rd party (e.g., a builder library) performs its op (serialization) and reports that it has used exactly 7 bytes. Now you call <code>v.resize(7)</code>. The problem: this will fill the first 7 bytes of the range with zeroes, wiping out the builder's work. Of course you could have solved this by keeping <code>.capacity() == .size() == 10</code> at all times and marking down the serialization's size in a separate variable. That's a bit sad though: you have a perfectly nice potential holder of the size, the <code>vector</code> inner <code>m_size</code> (or whatever it is called), but you cannot use it due to the value-initializing behavior of <code>vector</code>.</p>
<p >The link <a href="https://stackoverflow.com/questions/21028299/is-this-behavior-of-vectorresizesize-type-n-under-c11-and-boost-container/21028912#21028912">https://stackoverflow.com/questions/21028299/is-this-behavior-of-vectorresizesize-type-n-under-c11-and-boost-container/21028912#21028912</a> contains a nice solution in the form of an allocator adaptor which can be set as the <code><a class="el" href="classAllocator.html">Allocator</a></code> template param of a <code>vector</code> (etc.), adapting the normal allocator used; whether that's the typical heap-allocating <code>std::allocator</code> or something more exotic (e.g., SHM-allocating <a class="el" href="classipc_1_1shm_1_1stl_1_1Stateless__allocator.html" title="Stateless allocator usable with STL-compliant containers to store (or merely read) them directly in S...">ipc::shm::stl::Stateless_allocator</a>). cppreference.com <code>vector::resize()</code> page even links to that in "Notes" about this problem!</p>
<h3>What it does (what/how)</h3>
<p >The allocator works as follows: Suppose the STL-compliant container, having raw-allocated a buffer <code>sizeof(T)</code> bytes long, at <code>void* p</code>, requests to in-place-construct a <code>T{}</code> (a/k/a <code>T()</code>) at <code>p</code>. That is what, e.g., <code>vector::resize(size_t)</code> does: it <em>value-initializes</em> the <code>T</code> at address <code>p</code>. Now there are two possibilities:</p><ul>
<li><code>T</code> is a PoD (plain old data-type): value-initialization <code>T t{};</code> <em>does not equal</em> default-initialization <code>T t;</code>. The latter leaves pre-existing (possibly garbage) in <code>t</code>. The former does a deterministic initialization; e.g., if <code>T</code> is <code>int</code> then it zeroes <code>t</code>.</li>
<li><code>T</code> is not a PoD: value-initialization <code>T t{}</code> <em>does</em> equal default-initialization <code>T t</code>. They will both default-construct <code>T</code> via <code>T::T()</code>.</li>
</ul>
<p >The allocator adaptor changes the <em>former</em> bullet point to act like the <em>latter</em> bullet point; while making no changes for non-PoD <code>T</code>s. For PoD <code>T</code>s: The container will ask to do <code>T t{}</code> (a/k/a <code>T t = T()</code>), but what will happen in reality is <code>T t;</code> instead. Or, in plainer English, it's gonna make it so that <code>int</code>s and <code>float</code>s and simple <code>struct</code>s and stuff don't get auto-zeroed by containers when default-creating them on one's behalf.</p>
<h3>What about <code>flow::util::Blob</code>?</h3>
<p ><code>Blob</code>, as of this writing, specifically exists &ndash; among a few other reasons &ndash; to avoid the default-zeroing that a <code>vector&lt;uint8_t&gt;</code> would do. So why this <a class="el" href="classipc_1_1util_1_1Default__init__allocator.html" title="Allocator adaptor (useful for, e.g., vector that skips zero-filling) that turns a value-initializatio...">Default_init_allocator</a> then? Indeed! You should use <code>Blob</code> instead of <code>vector&lt;uint8_t&gt;</code>; you'll get quite a few goodies as a result. The problem: <code>Blob</code> always heap-allocates. Update: But, happily, now <code>flow::util::Basic_blob&lt;<a class="el" href="classAllocator.html">Allocator</a>&gt;</code> exists (and <code>Blob</code> is a heap-allocating case of it) and plays well with SHM-allocating allocators. Therefore <a class="el" href="classipc_1_1util_1_1Default__init__allocator.html" title="Allocator adaptor (useful for, e.g., vector that skips zero-filling) that turns a value-initializatio...">Default_init_allocator</a> is less useful.</p>
<dl class="todo"><dt><b><a class="el" href="todo.html#_todo000017">Todo:</a></b></dt><dd><a class="el" href="classipc_1_1util_1_1Default__init__allocator.html" title="Allocator adaptor (useful for, e.g., vector that skips zero-filling) that turns a value-initializatio...">ipc::util::Default_init_allocator</a> should be moved into Flow's <code>flow::util</code>.</dd></dl>
<h3>Source</h3>
<p >I (ygoldfel) took it from the above link(s), made stylistic edits, and added documentation. This is legal.</p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
  <table class="tparams">
    <tr><td class="paramname">T</td><td>The type managed by this instance of the allocator. See <code><a class="el" href="classAllocator.html">Allocator</a></code> concept (in standard or cppreference.com). </td></tr>
    <tr><td class="paramname"><a class="el" href="classAllocator.html">Allocator</a></td><td>The <code><a class="el" href="classAllocator.html">Allocator</a></code> being adapted. Usually one uses the heap-allocating <code>std::allocator</code> which is the default arg for STL-compliant containers usually. However it may well be something more advanced such as SHM-allocating <a class="el" href="classipc_1_1shm_1_1stl_1_1Stateless__allocator.html" title="Stateless allocator usable with STL-compliant containers to store (or merely read) them directly in S...">ipc::shm::stl::Stateless_allocator</a> or alias <a class="el" href="namespaceipc_1_1shm_1_1classic.html#ae783c814337577d6e06a65e1b43de043" title="Convenience alias for a shm::stl::Stateless_allocator&gt; w/r/t Pool_arena; use with Pool_arena_activato...">ipc::shm::classic::Pool_arena_allocator</a>. </td></tr>
  </table>
  </dd>
</dl>

<p class="definition">Definition at line <a class="el" href="default__init__allocator_8hpp_source.html#l00088">88</a> of file <a class="el" href="default__init__allocator_8hpp_source.html">default_init_allocator.hpp</a>.</p>
</div><h2 class="groupheader">Member Function Documentation</h2>
<a id="a56c4ab4a0b12a26f177c35d50b96f27f" name="a56c4ab4a0b12a26f177c35d50b96f27f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a56c4ab4a0b12a26f177c35d50b96f27f">&#9670;&nbsp;</a></span>construct() <span class="overload">[1/2]</span></h2>

<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename T , typename <a class="el" href="classAllocator.html">Allocator</a> &gt; </div>
<div class="memtemplate">
template&lt;typename U &gt; </div>
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void <a class="el" href="classipc_1_1util_1_1Default__init__allocator.html">ipc::util::Default_init_allocator</a>&lt; T, <a class="el" href="classAllocator.html">Allocator</a> &gt;::construct </td>
          <td>(</td>
          <td class="paramtype">U *&#160;</td>
          <td class="paramname"><em>ptr</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">noexcept</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Satisfies <code><a class="el" href="classAllocator.html">Allocator</a></code> concept optional requirement for in-place construction: specialized for 0-args, i.e., value-initialization; replaces value-initialization with default-initialization. </p>
<p >This specialization is the reason <a class="el" href="classipc_1_1util_1_1Default__init__allocator.html" title="Allocator adaptor (useful for, e.g., vector that skips zero-filling) that turns a value-initializatio...">Default_init_allocator</a> exists at all.</p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
  <table class="tparams">
    <tr><td class="paramname">U</td><td>Type being constructed. See <code><a class="el" href="classAllocator.html">Allocator</a></code> concept docs. </td></tr>
  </table>
  </dd>
</dl>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">ptr</td><td>Address at which to in-place-construct the <code>U</code>. </td></tr>
  </table>
  </dd>
</dl>

<p class="definition">Definition at line <a class="el" href="default__init__allocator_8hpp_source.html#l00132">132</a> of file <a class="el" href="default__init__allocator_8hpp_source.html">default_init_allocator.hpp</a>.</p>

</div>
</div>
<a id="ac030ddf85696b12dc0429b25bf5c6100" name="ac030ddf85696b12dc0429b25bf5c6100"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac030ddf85696b12dc0429b25bf5c6100">&#9670;&nbsp;</a></span>construct() <span class="overload">[2/2]</span></h2>

<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename T , typename <a class="el" href="classAllocator.html">Allocator</a> &gt; </div>
<div class="memtemplate">
template&lt;typename U , typename... Args&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">void <a class="el" href="classipc_1_1util_1_1Default__init__allocator.html">ipc::util::Default_init_allocator</a>&lt; T, <a class="el" href="classAllocator.html">Allocator</a> &gt;::construct </td>
          <td>(</td>
          <td class="paramtype">U *&#160;</td>
          <td class="paramname"><em>ptr</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">Args &amp;&amp;...&#160;</td>
          <td class="paramname"><em>args</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Satisfies <code><a class="el" href="classAllocator.html">Allocator</a></code> concept optional requirement for in-place construction: non-specialized version invoked for 1+ args. </p>
<p >This behaves identically to the adaptee allocator.</p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
  <table class="tparams">
    <tr><td class="paramname">U</td><td>Type being constructed. See <code><a class="el" href="classAllocator.html">Allocator</a></code> concept docs. </td></tr>
    <tr><td class="paramname">Args</td><td>Constructor args (1+ of them). </td></tr>
  </table>
  </dd>
</dl>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">ptr</td><td>Address at which to in-place-construct the <code>U</code>. </td></tr>
    <tr><td class="paramname">args</td><td>See <code>Args...</code>. </td></tr>
  </table>
  </dd>
</dl>

<p class="definition">Definition at line <a class="el" href="default__init__allocator_8hpp_source.html#l00140">140</a> of file <a class="el" href="default__init__allocator_8hpp_source.html">default_init_allocator.hpp</a>.</p>

</div>
</div>
<hr/>The documentation for this class was generated from the following file:<ul>
<li>util/<a class="el" href="default__init__allocator_8hpp_source.html">default_init_allocator.hpp</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Thu May 2 2024 23:56:39 for Flow-IPC by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.4
</small></address>
</body>
</html>
